package com.tianyu.inspection.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tianyu.inspection.common.model.PageModel;
import com.tianyu.inspection.service.api.IInfrastructureTypeService;
import com.tianyu.inspection.entity.InfrastructureType;
import com.tianyu.inspection.entity.model.OperateMo;
import com.tianyu.inspection.entity.model.QueryMo;
import com.tianyu.inspection.mapper.InfrastructureTypeMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**********************************
 * @Author YSW
 * @Description 基础设施服务
 * @Date 2021/1/18 - 09:35
 **********************************/

@Service
public class InfrastructureTypeServiceImpl implements IInfrastructureTypeService {

    @Resource
    private InfrastructureTypeMapper infrastructureTypeMapper;

    /**
     * 分页查询
     *
     * @param queryMo 参数模型
     * @return ModelMap
     */
    @Override
    public IPage<InfrastructureType> searchListPage(QueryMo queryMo) {
        return PageModel.getModelPage(queryMo,infrastructureTypeMapper);
    }


    /**
     * 通过ID等参数查询一条数据
     *
     * @param queryMo 参数集[实体ID 与 项目ID, 数据状态等]
     * @return 单条记录结果
     */
    @Override
    public InfrastructureType selectOne(QueryMo queryMo) {
        if(Objects.nonNull(queryMo.getEqMap().get("id"))){
            return infrastructureTypeMapper.selectOne(new QueryWrapper<InfrastructureType>().allEq(queryMo.getEqMap()));
        }
        return null;
    }

    /**
     * 插入一条数据
     *
     * @param operateMo 实体对象
     * @return 是否操作成功
     */
    @Override
    public boolean insert(OperateMo operateMo) {
        ObjectMapper objectMapper = new ObjectMapper();
        InfrastructureType infrastructureType = objectMapper.convertValue(operateMo.getEntity(), InfrastructureType.class);
        return 1 == infrastructureTypeMapper.insert(infrastructureType);
    }

    /**
     * 通过ID删除一条数据
     *
     * @param queryMo 参数集[实体ID 与 项目ID, 数据状态等]
     * @return 是否操作成功
     */
    @Override
    public boolean delete(QueryMo queryMo) {
        if(Objects.nonNull(queryMo.getEqMap().get("id"))){
            return 1 == infrastructureTypeMapper.delete(new QueryWrapper<InfrastructureType>().allEq(queryMo.getEqMap()));
        }
        return false;
    }

    /**
     * 插入一条数据
     *
     * @param operateMo 更新参数集
     * @return ModelMap
     */
    @Override
    public boolean update(OperateMo operateMo) {
        ObjectMapper objectMapper = new ObjectMapper();
        InfrastructureType infrastructureType = objectMapper.convertValue(operateMo.getEntity(), InfrastructureType.class);
        return 1 == infrastructureTypeMapper.update(infrastructureType,new QueryWrapper<InfrastructureType>().allEq(operateMo.getEqMap()));
    }

    /**
     * 通过ID逻辑删除一条数据
     *
     * @param queryMo 参数集[实体ID 与 项目ID, 数据状态]
     * @return 是否操作成功
     */
    @Override
    public boolean logicallyDelete(QueryMo queryMo) {
        return 1 == infrastructureTypeMapper.update(
                new InfrastructureType().setState(1),
                new QueryWrapper<InfrastructureType>().allEq(queryMo.getEqMap())
        );
    }
}
